package com.nbjtjc.safe.service.safe;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.common.bean.RECORDSTATUSENUM;
import com.nbjtjc.common.bean.TASKSTATUS;
import com.nbjtjc.safe.bean.Constant;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.safe.SafeDangerInspectTaskMapper;
import com.nbjtjc.safe.mapper.safe.SafeDangerInspectTaskRecordMapper;
import com.nbjtjc.safe.mapper.sys.SysFileMapper;
import com.nbjtjc.safe.model.danger.DangerInspectListPublic;
import com.nbjtjc.safe.model.safe.SafeDangerInspectTask;
import com.nbjtjc.safe.model.safe.SafeDangerInspectTaskRecord;
import com.nbjtjc.safe.model.sys.SysFile;
import com.nbjtjc.safe.util.MyShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class SafeDangerInspectTaskRecordServiceImpl {
    @Autowired
    private SafeDangerInspectTaskRecordMapper taskRecordMapper;

    @Autowired
    private SysFileMapper sysFileMapper;

    @Autowired
    private SafeDangerInspectTaskMapper taskMapper;
    @Value("${aliyun.oss.domain}")
    private String ossDomain;

    @Transactional(rollbackFor = Throwable.class)
    public Result add(SafeDangerInspectTaskRecord taskRecord) {
        Result result = new Result();
        taskRecord.setCreateTime(new Date());
        taskRecordMapper.insertSelective(taskRecord);
        return result;
    }

    public Result getTaskRecordInfo( ) {
        Result result = new Result();
        SafeDangerInspectTaskRecord taskRecord=new SafeDangerInspectTaskRecord();
        taskRecord.setActive(1);
        taskRecord.setActionUserId(MyShiroUtils.getSessionUserId());
        taskRecord.setStatus(RECORDSTATUSENUM.MEASURE.getCode());
        int measureCount=taskRecordMapper.selectCount(taskRecord);
        taskRecord.setStatus(RECORDSTATUSENUM.REFORM.getCode());
        int reformCount=taskRecordMapper.selectCount(taskRecord);
        taskRecord.setStatus(RECORDSTATUSENUM.ACCEPT.getCode());
        int acceptCount=taskRecordMapper.selectCount(taskRecord);
        Map<String,Integer> recordCountInfo=new HashMap<>();
        recordCountInfo.put("measure",measureCount);
        recordCountInfo.put("reform",reformCount);
        recordCountInfo.put("accept",acceptCount);
        result.setData(recordCountInfo);
        return result;
    }


    public Result doTask(SafeDangerInspectTaskRecord taskRecord){
        Result result=new Result();
        SafeDangerInspectTaskRecord oldRecord=taskRecordMapper.selectByPrimaryKey(taskRecord.getId());
        oldRecord.setActive(0);
        taskRecordMapper.updateByPrimaryKeySelective(oldRecord);
        taskRecord.setId(null);
        taskRecord.setCreateTime(new Date());
        taskRecord.setRecordExplain();
        taskRecordMapper.insert(taskRecord);
        result.setData(taskRecord);
        return result;
    }

    public Result search(SafeDangerInspectTaskRecord taskRecord){
        Result result=new Result();
        PageHelper.startPage(taskRecord.getPageCurrent(),taskRecord.getPageSize());
        List<SafeDangerInspectTaskRecord> taskRecords= taskRecordMapper.search(taskRecord);
        if(CollectionUtil.isNotEmpty(taskRecords)){
            Integer taskId=taskRecords.get(0).getInspectTaskId();
            SafeDangerInspectTask task=taskMapper.selectByPrimaryKey(taskId);
            taskRecords.forEach(record->{
                if(ObjectUtil.isNotNull(task)){
                    record.setInspectUsername(task.getInspectUser());
                    record.setTaskName(task.getTaskName());
                }
            });
            comparePicsByInspectListId(taskRecords);
        }
        result.setData(new PageInfo<>(taskRecords));
        return result;
    }




    private void comparePicsByInspectListId(List<SafeDangerInspectTaskRecord> taskRecordList){
        List<Integer> listIds = taskRecordList.parallelStream()
                .map(SafeDangerInspectTaskRecord::getInspectTablePublicListId).collect(Collectors.toList());
        List<SysFile> sysFiles = sysFileMapper.findByContentIdsAllType(listIds);
        Set<SafeDangerInspectTaskRecord> recordSet= new TreeSet<>(
        (o1,o2)->o1.getInspectTablePublicListId().compareTo(o2.getInspectTablePublicListId()));
        recordSet.addAll(taskRecordList);
        List<SafeDangerInspectTaskRecord> uniqRecord=new ArrayList<>(recordSet);
        Map<Integer,SafeDangerInspectTaskRecord> idMap=  uniqRecord.stream()
                .collect(Collectors.toMap(SafeDangerInspectTaskRecord::getInspectTablePublicListId, listPublic -> listPublic));
        sysFiles.forEach(file->{
            file.setUrl(ossDomain+"/"+file.getPath()+"?x-oss-process=image/resize,m_fixed,h_70,w_70");
            file.setOriUrl(ossDomain+"/"+file.getPath());
            if(file.getContentType().equals(Constant.SysFileType.TASK_TABLE_LIST_FILE.getValue())){
                idMap.get(file.getContentId()).getDangerPics().add(file);
            }
            if(file.getContentType().equals(Constant.SysFileType.TASK_REFORM_FILE.getValue())){
                idMap.get(file.getContentId()).getReformPics().add(file);
            }
            if(file.getContentType().equals(Constant.SysFileType.TASK_RECORD_REFORM_ACCEPT_FILE.getValue())){
                idMap.get(file.getContentId()).getAcceptPics().add(file);
            }
            if(file.getContentType().equals(Constant.SysFileType.TASK_RECORD_CASE_FILE.getValue())){
                idMap.get(file.getContentId()).getDangerCasePics().add(file);
            }
        });
        taskRecordList.forEach(taskRecord -> {
            taskRecord.setDangerPics(idMap.get(taskRecord.getInspectTablePublicListId()).getDangerPics());
            taskRecord.setReformPics(idMap.get(taskRecord.getInspectTablePublicListId()).getReformPics());
            taskRecord.setAcceptPics(idMap.get(taskRecord.getInspectTablePublicListId()).getAcceptPics());
            taskRecord.setDangerCasePics(idMap.get(taskRecord.getInspectTablePublicListId()).getDangerCasePics());
        });
    }

}
